package com.lanhai.crowd.mvc.handler;

import com.lanhai.crowd.entity.Admin;
import com.lanhai.crowd.service.api.AdminService;
import com.lanhai.crowd.util.CrowdUtil;
import com.lanhai.crowd.util.ResultEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
public class TestSSM {

    @Autowired
    private AdminService adminService;

    private Logger logger = LoggerFactory.getLogger(TestSSM.class);

    @RequestMapping("/test/ssm.html")
    public String testSSM(ModelMap modelMap, HttpServletRequest req) {
        List<Admin> list = adminService.getAll();
        modelMap.addAttribute("adminList", list);

        // 判断请求方式
        boolean requestType = CrowdUtil.jundgeRequestType(req);
        logger.info("requestType = " + requestType);
        System.out.println(10 / 0);

        return "target";
    }

    @RequestMapping("/send/array.html")
    @ResponseBody
    public String testReceiveArrayOne(@RequestParam("array") List<Integer> array){
        for (Integer number : array) {
            logger.info("number = " + number);
        }
        return "success";
    }

    @RequestMapping("/send/map.json")
    @ResponseBody
    public ResultEntity<Map<String, String>> testReceiveArrayOne(
            @RequestBody Map<String, String> map, HttpServletRequest req){

        // 判断请求方式
        boolean requestType = CrowdUtil.jundgeRequestType(req);
        logger.info("requestType = " + requestType);

        for (String s : map.keySet()) {
            logger.info(s + "-" + map.get(s));
        }

        String a = null;
        System.out.println(a.length());

        return ResultEntity.data(map);
    }
}
